<template>
  <ul class="wrap" >
    <li v-for="(item,key) in cities" :key='key' @click="jump" @touchstart="hadleTouchStart"
    @touchmove="hadleTouchMove"
    @touchend="hadleTouchEnd"
    :ref="key"
    >{{key}}</li>
  </ul>
</template>

<script>
export default {
  name: 'CityAlphabet',
  props: {
    cities: Object
  },
  computed: {
    letters () {
      const letters = []
      for (let key in this.cities) {
        letters.push(key)
      }
      return letters
    }
  },
  data () {
    return {
      touchStatus: false,
      startY: 0,
      timer: null
    }
  },
  updated () {
    this.startY = this.$refs[this.letters[0]][0].offsetTop
  },
  methods: {
    jump (e) {
      this.$emit('change', e.target.innerHTML)
    },
    hadleTouchStart () {
      this.touchStatus = true
    },
    hadleTouchMove (e) {
      if (this.timer) {
        clearTimeout(this.timer)
      }
      if (this.touchStatus) {
        this.timer = setInterval(() => {
          const touchY = e.touches[0].clientY - this.startY
          const index = Math.floor(touchY / 20)
          if (index >= 0 && index < this.letters.length) {
            this.$emit('change', this.letters[index])
          }
        }, 5)
      }
    },
    hadleTouchEnd () {
      this.touchStatus = false
    }
  }
}
</script>
<style lang="stylus" scoped>
   @import '~styles/varibles.styl'
    .wrap
      display: flex;
      flex-direction:column;
      justify-content: center;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      color: $bgColor;
      li
        height: .4rem;
</style>
